package com.wzc.hc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzc.hc.entity.po.Park;
import com.wzc.hc.entity.po.ParkingRecord;
import com.wzc.hc.entity.resp.Result;
import com.wzc.hc.entity.tool.BeanConverter;
import com.wzc.hc.entity.vo.ParkVo;
import com.wzc.hc.service.ParkService;
import com.wzc.hc.service.ParkingRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
    @author: m1825
    @date: 2025/3/6 17:03
*/
@Tag(name = "停车场")
@RestController
@RequestMapping("/park")
public class ParkController {
    @Resource
    private ParkService parkService;
    @Resource
    private ParkingRecordService parkingRecordService;

    @Operation(summary = "新增")
    @PostMapping("/add")
    public Result<Object> add(@RequestBody ParkVo parkVo) {
        try {
            parkService.save(BeanConverter.convertVoToPo(parkVo, Park.class));
            return Result.ok();
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @Operation(summary = "删除")
    @DeleteMapping("/delete")
    public Result<Object> delete(@RequestParam(value = "ParkId") String id) {
        try {
            parkService.removeById(id);
            return Result.ok();
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @Operation(summary = "更新")
    @PutMapping("/update")
    public Result<Object> update(@RequestBody ParkVo parkVo) {
        try {
            Park park = BeanConverter.convertVoToPo(parkVo, Park.class);
            parkService.updateById(park);
            return Result.ok();
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @Operation(summary = "查询")
    @PostMapping("/search")
    public Result<Page<ParkVo>> search(@RequestBody ParkVo parkVo,
                                       @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            Page<Park> page = new Page<>(pageNum, pageSize);
            QueryWrapper<Park> parkQueryWrapper = new QueryWrapper<>();
            if (parkVo.getSerial() != null)
                parkQueryWrapper.eq("serial", parkVo.getSerial());
            if (parkVo.getType() != null)
                parkQueryWrapper.eq("type", parkVo.getType());
            Page<Park> parkPage = parkService.page(page, parkQueryWrapper);
            List<ParkVo> parkVos = BeanConverter.converList(parkPage.getRecords(), ParkVo.class);
            Page<ParkVo> parkVoPage = new Page<>(pageNum, pageSize, parkPage.getTotal());
            parkVoPage.setRecords(parkVos);
            return Result.ok(parkVoPage);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @Operation(summary = "车辆进场")
    @PostMapping("/carIn")
    public Result<Object> carIn(@RequestBody ParkingRecord parkingRecord) {
        try {
            parkingRecordService.save(parkingRecord);
            return Result.ok();
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @Operation(summary = "车辆离场")
    @PostMapping("/carOut")
    public Result<Object> carOut(@RequestParam(value = "licensePlateNumber") String licensePlateNumber) {
        try {
            QueryWrapper<ParkingRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("license_plate_number", licensePlateNumber);
            queryWrapper.eq("vehicle_state", "进场");
            ParkingRecord one = parkingRecordService.getOne(queryWrapper);
            one.setVehicleState("离场");
            one.setGmtModified(null);
            parkingRecordService.updateById(one);
            return Result.ok();
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @Operation(summary = "进场信息")
    @GetMapping("/carRecordPage")
    public Result<Page<ParkingRecord>> carRecordPage(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                     @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            Page<ParkingRecord> parkingRecordPage = new Page<>(pageNum, pageSize);
            Page<ParkingRecord> page = parkingRecordService.page(parkingRecordPage);
            return Result.ok(page);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @Operation(summary = "进场信息查询")
    @PostMapping("/carRecordSearch")
    public Result<Page<ParkingRecord>> carRecordSearch(@RequestBody ParkingRecord parkingRecord,
                                                       @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                       @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            Page<ParkingRecord> parkingRecordPage = new Page<>(pageNum, pageSize);
            QueryWrapper<ParkingRecord> queryWrapper = new QueryWrapper<>();
            if (parkingRecord.getLicensePlateNumber() != null)
                queryWrapper.eq("license_plate_number", parkingRecord.getLicensePlateNumber());
            if (parkingRecord.getVehicleState() != null)
                queryWrapper.eq("vehicle_state", parkingRecord.getVehicleState());
            if (parkingRecord.getLicensePlateType() != null)
                queryWrapper.eq("license_plate_type", parkingRecord.getLicensePlateType());
            Page<ParkingRecord> page = parkingRecordService.page(parkingRecordPage, queryWrapper);
            return Result.ok(page);
        } catch (Exception e) {
            return Result.fail();
        }
    }
}